$().ready(function() {
    $('#cars').tagsinput({
        itemValue: 'value',
        itemText: 'text',
        trimValue: true,
        allowDuplicates: true
    });
	validateRule();
});
$.validator.setDefaults({
	submitHandler : function() {
		save();
	}
});

function save() {
	$.ajax({
		cache : true,
		type : "POST",
		url : "/flowLine/manage/save",
		data : $('#signupForm').serialize(),// 你的formid
		async : false,
		error : function(request) {
			parent.layer.alert("Connection error");
		},
		success : function(data) {
			if (data.code == 0) {
				parent.layer.msg("操作成功");
				parent.reLoad();
				var index = parent.layer.getFrameIndex(window.name); // 获取窗口索引
				parent.layer.close(index);
			} else {
				if (data.msg) {
					parent.layer.alert(data.msg)
				}else {
					window.location.href = "/";
				}
			}

		}
	});
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#signupForm").validate({
		rules : {
			flowName : {
				required : true
			}
		},
		messages : {
			title : {
				required : icon + "流水线名称"
			}
		}
	})
}

function selectCars() {
    layer.open({
        id:'select_cars',
        type: 2,
        title: '选择线路',
        maxmin: true,
        shadeClose: false, // 点击遮罩关闭层
        area : [ '60%', '80%' ],
        content: "/flowLine/manage" + '/selectCars' // iframe的url
    });
}

/**
 * 加载线路信息
 * @param carId
 * @param carName
 */
function loadCarInfo(carId, carName) {
	if (carId && carName) {
        $('#cars').tagsinput('add', { "value": carId , "text": carName});
	}
}